﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Constants;
using mshtml;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class FightWithMonsterChainItem : BotvaLogedInChainItemBase
    {
        protected override void PrepareRequestInternal()
        {
            Url = "http://g3.botva.ru/dozor.php";
            Method = HttpMethod.POST;
            AddGet("a", "attack");

            string content = Chain.LastContent;
            content = content.Replace(Environment.NewLine, string.Empty);
            while (content.IndexOf("  ") >= 0)
            {
                content = content.Replace("  ", " ");
            }
            content = content.ToUpper();

            IHTMLDocument2 html = new HTMLDocumentClass();
            html.write(content);
            html.close();

            IHTMLElementCollection all = html.body.all as IHTMLElementCollection;

            foreach (IHTMLElement item in all)
            {
                if (item.tagName.ToUpper() != "FORM")
                    continue;

                if (item.getAttribute("action", 0).ToString() != "")
                    continue;

                if (item.getAttribute("action", 0).ToString().Trim().ToUpper() != "?a=attack".Trim().ToUpper())
                    continue;

                if (!item.innerHTML.ToUpper().Contains("НАПАСТЬ"))
                    continue;

                IHTMLElementCollection inputs = (item.children as IHTMLElementCollection).tags("INPUT") as IHTMLElementCollection;
                List<IHTMLInputElement> inps = new List<IHTMLInputElement>(inputs.Cast<IHTMLInputElement>());

                bool isOk = false;

                foreach (IHTMLElement input in inputs)
                {
                    IHTMLInputElement inp = input as IHTMLInputElement;

                    if (inp.name != null && inp.name.Trim().ToUpper() == "K")
                    {
                        AddPost("k", input.getAttribute("VALUE", 0).ToString().ToLower());
                        isOk = true;
                        break;
                    }
                }

                if (isOk)
                    break;
            }
        }
    }
}

/*
<form action="?a=attack" class="inline" method="post"><input type="hidden" value="49595" name="k"><input type="submit" value="НАПАСТЬ" class="cmd_all cmd_row3 cmd_arow3 "></form>

<form action="?a=leave" class="inline" method="post"><input type="hidden" value="49595" name="k"><input type="submit" value="ВЫЙТИ ИЗ ПОДЗЕМЕЛЬЯ" class="cmd_all cmd_red cmd_ared "></form>

POST /dozor.php?a=attack HTTP/1.1
Host: g3.botva.ru
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20100101 Firefox/6.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*_/*;q=0.8
Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://g3.botva.ru/dozor.php?a=monster
Cookie: server=3; PHPSESSID=7do07g1v8m8hm0ts43ksant7l5; uid=133096

k	49595
*/